package com.example.service.impl;

import ch.qos.logback.classic.Logger;
import com.example.entity.MaintenancePlan;
import com.example.mapp.MaintenancePlanMapp;
import com.example.mapp.PromotionMapp;
import com.example.service.MaintenancePlanService;
import com.example.util.PromotionUtil;
import com.example.util.ResultUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MaintenancePlanServiceImpl  implements MaintenancePlanService {

    private final static Logger logger = (Logger) LoggerFactory.getLogger(MaintenancePlanServiceImpl.class);

    @Autowired
    private MaintenancePlanMapp maintenancePlanMapp;

    @Autowired
	private PromotionMapp promotionMapp;

    @Override
    public Map<String, Object> queryList(MaintenancePlan maintenancePlan) {
        List<MaintenancePlan> list =null;
        Map<String,Object> map = new HashMap<String,Object>();
        try {
            Page<MaintenancePlan> page = PageHelper.startPage(maintenancePlan.getCurrentPage(), maintenancePlan.getPageSize());
            list = maintenancePlanMapp.queryList(maintenancePlan);
            map.put("list",list);
            map.put("total",page.getTotal());

        } catch (Exception e) {
            logger.error(e.getMessage());
            return ResultUtil.returnError("查询失败"+e.getMessage(),null);
        }
        return ResultUtil.returnSuccess("查询成功",map);
    }

    @Override
    public Map<String, Object> queryById(String id) {
        MaintenancePlan maintenancePlan = null;
        try {
            maintenancePlan = maintenancePlanMapp.queryById(id);
        }catch (Exception e){
            logger.error(e.getMessage());
            return ResultUtil.returnError(e.getMessage(),null);
        }
        return ResultUtil.returnSuccess("成功",maintenancePlan);
    }

    @Override
    @Transactional
    public Map<String, Object> deleteById(String id) {
        try {
            MaintenancePlan maintenancePlan = maintenancePlanMapp.queryById(id);
            // 积分有问题
            /*PromotionUtil.calcParty(promotionMapp, -30, "1", Long.valueOf(maintenancePlan.getPartyId()), "删除1分年度维保计划", "0");*/
            int i = maintenancePlanMapp.deleteById(id);
        }catch (Exception e){
            //手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            logger.error(e.getMessage());
            return ResultUtil.returnError(e.getMessage(),null);
        }
        return ResultUtil.returnSuccess("成功",null);
    }

    @Override
    @Transactional
    public Map<String, Object> insert(MaintenancePlan maintenancePlan) {
        try {
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
            maintenancePlan.setStartDate( sf.format(new Date()));
            int i = maintenancePlanMapp.insert(maintenancePlan);
            // 积分写的有问题
            /*PromotionUtil.calcParty(promotionMapp, 30, "1", Long.valueOf(maintenancePlan.getPartyId()), "上传1分年度维保计划", "0");*/
        }catch (Exception e){
        	TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            logger.error(e.getMessage());
            return ResultUtil.returnError(e.getMessage(),null);
        }
        return ResultUtil.returnSuccess("成功",null);
    }
}
